using System;
using System.Collections.Generic;

namespace IWS.Lib.SingleSignOn
{
	public class Service
	{
		string m_ID;
		string m_KeyFile;
		DateTime m_LastActiveDate;
		DateTime m_ExpirationDate;
		DateTime m_CreateDate;

		/// <summary>
		/// Create a new service
		/// </summary>
		public Service()
		{
			init();
		}

		/// <summary>
		/// Create a new instance and preload data by ID.
		/// </summary>
		/// <param name="id"></param>
		public Service(string id)
		{
			init();
			Load(id);
		}


		private void init()
		{
			 m_ID = "";
			 m_KeyFile = "";
			 m_LastActiveDate = new DateTime(1900,1,1,0,0,0,0);
			 m_ExpirationDate = new DateTime(1900,1,1,0,0,0,0);
			 m_CreateDate = new DateTime(1900,1,1,0,0,0,0);
		}


		#region Properties
		public string ID
		{
			get {return m_ID;}
			set {m_ID = value;}
		}
		public string KeyFile
		{
			get {return m_KeyFile;}
			set {m_KeyFile = value;}
		}
		public DateTime LastActiveDate
		{
			get {return m_LastActiveDate;}
			set {m_LastActiveDate = value;}
		}
		public DateTime ExpirationDate
		{
			get {return m_ExpirationDate;}
			set {m_ExpirationDate = value;}
		}
		public DateTime CreateDate
		{
			get {return m_CreateDate;}
			set {m_CreateDate = value;}
		}
		#endregion

		/// <summary>
		/// Load this service
		/// </summary>
		public void Load(string id)
		{
			Service data = Instance.getDataProvider().LoadService(id);
			this.ID = data.ID;
			this.KeyFile = data.KeyFile;
			this.LastActiveDate = data.LastActiveDate;
			this.ExpirationDate = data.ExpirationDate;
			this.CreateDate = data.CreateDate;
		}

		/// <summary>
		/// Save this service
		/// </summary>
		/// <param name="id"></param>
		public void Save()
		{
			//TODO:	Finish ID generation code here.
			Instance.getDataProvider().SaveService(this);
		}

		/// <summary>
		/// Remove this service
		/// </summary>
		/// <param name="id"></param>
		public void Remove()
		{
			Instance.getDataProvider().RemoveService(this.ID);
		}

		/// <summary>
		/// Check if this service already exists.
		/// </summary>
		/// <param name="id">Service ID</param>
		public bool Exists()
		{
			return Service.Exists(this.ID);
		}

		/// <summary>
		/// REmove a service
		/// </summary>
		/// <param name="ID"></param>
		public static void Remove(string ID)
		{
			Instance.getDataProvider().RemoveService(ID);
		}

		/// <summary>
		/// Check if this service already exists.
		/// </summary>
		/// <param name="id">Service ID</param>
		public static bool Exists(string ID)
		{
			return Instance.getDataProvider().CountService(ID) > 0;
		}

		/// <summary>
		/// List all Service
		/// </summary>
		public static List<Service> ListAll()
		{
			return Instance.getDataProvider().ListAllService();
		}

	}
}
